﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using mshtml;
using PlayerTest.Player.Data;



namespace PlayerTest.Player.Handler
{
    class ManageSMI
    {
        string typeSMI = null;

        Dictionary<int, SMIContent> dic_SMI = new Dictionary<int, SMIContent>();

        public void MergeSMI(string[] filesPath)
        {
            byte[] data = new byte[1024];

            try
            {

                int lengthByte = 0;

                bool isFirstFile = true;
                FileStream reader, writer;
                foreach (String filePath in filesPath)
                {
                    reader = new FileStream(filePath, FileMode.Open, FileAccess.Read);


                    if (isFirstFile)
                    {
                        writer = new FileStream(@"C:\Users\knssm\Desktop\6월창의과제\Files_Merge\MergeSmi.smi", FileMode.Create, FileAccess.Write);
                        isFirstFile = false;
                    }
                    else
                        writer = new FileStream(@"C:\Users\knssm\Desktop\6월창의과제\Files_Merge\MergeSmi.smi", FileMode.Append, FileAccess.Write);

                    while ((lengthByte = reader.Read(data, 0, data.Length)) != 0)
                        writer.Write(data, 0, lengthByte);

                    reader.Close();
                    writer.Close();
                }

            }

            catch (Exception Ex)
            {
                System.Windows.Forms.MessageBox.Show(Ex.Message);

            }
        }

        /// <summary>
        /// SMI 자막 파싱하여 저장
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="isClickOepnSmi"></param>
        public void ParsingSMI(string filePath, bool isClickOepnSmi)
        {
            //Encoding.UTF8 || Encoding.Default

            StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.Default);

            string allText = reader.ReadToEnd();

            HTMLDocument doc = new HTMLDocument();
            doc.designMode = "on";

            object[] oPageText = { allText };
            IHTMLDocument2 oMyDoc = (IHTMLDocument2)doc;
            oMyDoc.write(oPageText);

            int i = 0, key = 0, doc_size = 0;
            long time;
            string type = null, txt = null;

            doc_size = doc.getElementsByTagName("SYNC").length;


            IHTMLElement element = (IHTMLElement)doc.getElementsByTagName("P").item(0);
            type = element.getAttribute("className");
            element = (IHTMLElement)doc.getElementsByTagName("P").item(doc_size - 1);

            if (isClickOepnSmi)
            {
                typeSMI = SeperateSMI(type, element.getAttribute("className"));
                isClickOepnSmi = false;
            }


            while (i < doc_size)
            {
                element = (IHTMLElement)doc.getElementsByTagName("SYNC").item(i);
                time = long.Parse(element.getAttribute("start"));

                element = (IHTMLElement)doc.getElementsByTagName("P").item(i);
                type = element.getAttribute("className");
                txt = element.innerText;


                if (type.Equals(typeSMI))
                {
                    dic_SMI.Add(key, new SMIContent(time, type, txt));
                    key++;
                }
                else if (typeSMI.Equals("UNITED"))
                {
                    dic_SMI.Add(key, new SMIContent(time, type, txt));
                    key++;
                }

                i++;
            }

            reader.Close();


        }

        private string SeperateSMI(string first, string last)
        {
            if (!first.Equals(last))
            {
                return "UNITED";
            }
            else
            {
                if (first.Equals("KRCC"))
                    return "KRCC";
                else
                    return "ENCC";
            }

        }

        public Dictionary<int, SMIContent> DIC_SMI
        {
            get { return dic_SMI; }
            set { dic_SMI = value; }
        }

        public string TypeSMI
        {
            get { return typeSMI; }
            set { typeSMI = value; }
        }

    }
}
